﻿#region Librerias

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ESF.General.GUI;
using System.Threading;

#endregion


namespace ESF.WinForms.Dialogs
{
	public partial class ProgressDialog : Form, IProgressDialog<IWin32Window>
	{
		#region Miembros

		private bool _WasCancelled;

		public event Action OnCancel;

		#endregion


		#region Constructor

		public ProgressDialog()
		{
			InitializeComponent();

			HideWhenComplete = true;
			IsCancelable = false;
		}

		#endregion


		#region Methods

		public new void Hide ()
		{
			Invoke ((Action) delegate { base.Hide (); });
		}


		public new void Show (IWin32Window pParent)
		{
			base.Show (pParent);

			base.CenterToParent ();
		}


		public void Clear()
		{
			btnOK.Enabled = false;
			btnCancel.Enabled = false;

			_WasCancelled = false;

			WinFormsHelper.ClearAdvance (lblCurrentStepName, prgProgressBar);			
		}

		#endregion


		#region Form Events

		private void ProgressDialog_FormClosing(object sender, FormClosingEventArgs e)
		{
			e.Cancel = true;

			if (prgProgressBar.Value == 100)
			{
				Hide ();
			}
		}

		#endregion


		#region Button Events

		private void btnCancel_Click(object sender, EventArgs e)
		{
			if (OnCancel != null)
			{
				OnCancel ();
			}

			Hide ();
		}


		private void btnOK_Click(object sender, EventArgs e)
		{
			if (prgProgressBar.Value == 100)
			{
				Hide ();
			}
		}

		#endregion


		#region Interface Fields

		public bool WasCancelled
		{
			get { return _WasCancelled; }
		}

		public bool HideWhenComplete { set; get; }


		public bool IsCancelable
		{
			set { btnCancel.Enabled = value; }
			get { return btnCancel.Enabled; }
		}

		public double PercentageAdvance100
		{
			set 
			{ 
				WinFormsHelper.SetValue (prgProgressBar, value); 

				if (value == 100)
				{
					Invoke ((Action) delegate () 
					{
						btnOK.Enabled = true;
						btnOK.Focus ();
						btnCancel.Enabled = false;

						if (HideWhenComplete)
						{
							Hide ();
						}
					});
				}
			}
		}

		public string Title
		{
			set { WinFormsHelper.SetText (this, value); }
		}

		public string CurrentStepName
		{
			set { WinFormsHelper.SetText (lblCurrentStepName, value); }
		}

		#endregion
	}
}
